<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    showFooter
    :title="getTitle"
    width="50%"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm">
    </BasicForm>

  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicForm, useForm } from '@/components/Form';
  import { formSchema } from './role.data';
  import { BasicModal, useModalInner } from '@/components/Modal';

  import { BasicTree, TreeItem } from '@/components/Tree';
  import { saveGoodApi,goodDetailApi } from '@/api/sys/shop';


  import {useMessage} from "@/hooks/web/useMessage";

  const emit = defineEmits(['success', 'register']);
  const isUpdate = ref(true);
  const treeData = ref<TreeItem[]>([]);
  const { createMessage } = useMessage();

  const goodId =ref("");
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 90,
    baseColProps: { span: 24 },
    schemas: formSchema,
    showActionButtonGroup: false,
  });

  const [register, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //打开弹层后的回调
    resetFields();
    setModalProps({ confirmLoading: false });
    // 需要在setFieldsValue之前先填充treeData，否则Tree组件可能会报key not exist警告
    // if (unref(treeData).length === 0) {
    //   treeData.value = (await getMenuList()) as any as TreeItem[];
    // }
    isUpdate.value = !!data?.isUpdate;
    goodId.value="";
    if (unref(isUpdate)) {

      // console.log("---data.record:",data.record)
      goodId.value=data.record.goodId;
      goodDetailApi({
        goodId:goodId.value
      }).then(res=>{
        //console.log(res)
        if(res.code==200){
          getBooksFn(res.data)
        }
      })
      // setFieldsValue({
      //   ...data.record,
      // });
    }
  });

  const getTitle = computed(() => (!unref(isUpdate) ? '新增书本' : '编辑书本'));

  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      // TODO custom api
      values.goodId=goodId.value;
      
      // console.log(values);
      // return;
      //保存
      if(values.mainImgs){
        values.mainImg=values.mainImgs.join(",");
      }
      if(values.details){
        values.detail=values.details.join(",");
      }
      saveGoodApi(values).then((res) => {
        // console.log("---res::",res)
        createMessage.success(res.message);
      })
        .catch((res) => {
          createMessage.error(res.message);
        })
        .finally(() => {
          closeModal();
          emit('success');
        });


    } finally {
      setModalProps({ confirmLoading: false });
    }
  }

  function getBooksFn(item){

    if(item.mainImg){
      item.mainImgs=item.mainImg.split(",")
    }else{
      item.mainImgs=[];
    }
    if(item.detail){
      item.details=item.detail.split(",")
    }else{
      item.details=[];
    }
    //console.log("---item:",item)
    setFieldsValue(item);

  }

</script>
